import { inject, ref, watch } from 'vue'
import axios from 'axios'


const paramsRef = ref({
  template: '',
  windows: new Set()
})

const directionRef = ref('')

const templateOptions = [
  {
    value: 'AAA',
    label: 'AAA'
  },
  {
    value: 'Muscle',
    label: 'Muscle'
  }
]

const directionOptions = [
  {
    value: 'A',
    label: 'Anterior'
  },
  {
    value: 'P',
    label: 'Posterior'
  },
  {
    value: 'L',
    label: 'Left'
  },
  {
    value: 'R',
    label: 'Right'
  },
  {
    value: 'S',
    label: 'Superior'
  },
  {
    value: 'I',
    label: 'Inferior'
  }
]

const presetWindowsOptions = ref([
  {
    value: 'bone'
  },
  {
    value: 'blood'
  }
])

const chooseWindowTemplate = (windowName: string) => {
  if (paramsRef.value.windows.has(windowName)) {
    paramsRef.value.windows.delete(windowName)
  } else {
    paramsRef.value.windows.add(windowName)
  }
}


export const use3DParams = () => {
  const {
    ioInstance,
    baseUri,
    throughGatewayFlag,
    sendMsg
  } = inject('pyImgHandlerIo') as any
  const sendParams = async (params) => {
    const prefix = throughGatewayFlag ? `${baseUri}/image/` : `${baseUri}/`
    await axios.post(`${prefix}set3dParams`, {
      template: params.template,
      windows: Array.from(params.windows)
    })
  }
  const chooseDirection = async (direction) => {
    const prefix = throughGatewayFlag ? `${baseUri}/image/` : `${baseUri}/`
    await axios.post(`${prefix}set3dDirection`, {
      direction: direction
    })
  }

  watch(paramsRef, async (newVal, _) => {
    await sendParams(newVal)
  }, {
    deep: true
  })

  return {
    paramsRef,
    directionRef,
    templateOptions,
    directionOptions,
    presetWindowsOptions,
    chooseWindowTemplate,
    chooseDirection
  }
}
